package cn.iocoder.yudao.module.english.controller.admin.evaluationphrases;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.english.controller.admin.evaluationphrases.vo.EvaluationPhrasesPageReqVO;
import cn.iocoder.yudao.module.english.controller.admin.evaluationphrases.vo.EvaluationPhrasesRespVO;
import cn.iocoder.yudao.module.english.controller.admin.evaluationphrases.vo.EvaluationPhrasesSaveReqVO;
import cn.iocoder.yudao.module.english.dal.dataobject.evaluationphrases.EvaluationPhrasesDO;
import cn.iocoder.yudao.module.english.service.evaluationphrases.EvaluationPhrasesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 评价常用语")
@RestController
@RequestMapping("/english/evaluation-phrases")
@Validated
public class EvaluationPhrasesController {

    @Resource
    private EvaluationPhrasesService evaluationPhrasesService;

    @PostMapping("/create")
    @Operation(summary = "创建评价常用语")
    @PreAuthorize("@ss.hasPermission('english:evaluation-phrases:create')")
    public CommonResult<Long> createEvaluationPhrases(@Valid @RequestBody EvaluationPhrasesSaveReqVO createReqVO) {
        return success(evaluationPhrasesService.createEvaluationPhrases(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评价常用语")
    @PreAuthorize("@ss.hasPermission('english:evaluation-phrases:update')")
    public CommonResult<Boolean> updateEvaluationPhrases(@Valid @RequestBody EvaluationPhrasesSaveReqVO updateReqVO) {
        evaluationPhrasesService.updateEvaluationPhrases(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评价常用语")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('english:evaluation-phrases:delete')")
    public CommonResult<Boolean> deleteEvaluationPhrases(@RequestParam("id") Long id) {
        evaluationPhrasesService.deleteEvaluationPhrases(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评价常用语")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('english:evaluation-phrases:query')")
    public CommonResult<EvaluationPhrasesRespVO> getEvaluationPhrases(@RequestParam("id") Long id) {
        EvaluationPhrasesDO evaluationPhrases = evaluationPhrasesService.getEvaluationPhrases(id);
        return success(BeanUtils.toBean(evaluationPhrases, EvaluationPhrasesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评价常用语分页")
    @PreAuthorize("@ss.hasPermission('english:evaluation-phrases:query')")
    public CommonResult<PageResult<EvaluationPhrasesRespVO>> getEvaluationPhrasesPage(@Valid EvaluationPhrasesPageReqVO pageReqVO) {
        PageResult<EvaluationPhrasesDO> pageResult = evaluationPhrasesService.getEvaluationPhrasesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvaluationPhrasesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评价常用语 Excel")
    @PreAuthorize("@ss.hasPermission('english:evaluation-phrases:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEvaluationPhrasesExcel(@Valid EvaluationPhrasesPageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvaluationPhrasesDO> list = evaluationPhrasesService.getEvaluationPhrasesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "评价常用语.xls",
                "数据", EvaluationPhrasesRespVO.class,
                BeanUtils.toBean(list, EvaluationPhrasesRespVO.class));
    }

}